#include "DispatchHandler.h"
#include "JME_JsonFile.h"
#include "JME_RPCManager.h"
#include "GateServer.h"

#include "RPC_dispatch.pb.h"

#include "account.pb.h"

void DispatchHandler::init()
{
#define REG_MSG_HANDLER(msgId, msgHandler) regMessageHandler(msgId, boost::bind(&DispatchHandler::msgHandler, _1, _2))

	REG_MSG_HANDLER(client_request_login_MsgId_requestLogin, sendMessageToAccount);
	REG_MSG_HANDLER(client_request_join_game_MsgId_requestJoinGame, sendMessageToGame);

#undef REG_MSG_HANDLER
}

void DispatchHandler::sendMessageToAccount( JME_TcpSession::JME_TcpSessionPtr session, const JME_MessagePtr params )
{
	FUNCTIONLOG
	__TRY__
		RPC_RequestHandle proto;
		proto.set_msg_id(params->_msgId);
		proto.set_pid(params->_bindId);
		proto.set_net_id(session->getNetId());
		
		auto param = params->getMessageStr();
		proto.set_params(param);
		proto.set_gate(GateServer::getInstance()->_serverName);
		proto.set_server_id(GateServer::getInstance()->_serverId);

		GETRPC("account")->callRpcMethod("requestHandle", &proto);
	
	__CATCH_CHAR_ERROR__

		LOGE("%s with server [ account ]", e);

	__TRY_END__
}

void DispatchHandler::sendMessageToGame(JME_TcpSession::JME_TcpSessionPtr session, const JME_MessagePtr params)
{
	FUNCTIONLOG

	__TRY__
		RPC_RequestHandle proto;
		proto.set_msg_id(params->_msgId);
		proto.set_pid(params->_bindId);
		proto.set_net_id(session->getNetId());

		auto param = params->getMessageStr();
		proto.set_params(param);
		proto.set_gate(GateServer::getInstance()->_serverName);
		proto.set_server_id(GateServer::getInstance()->_serverId);

		GETRPC("game-server-1-1")->callRpcMethod("requestHandle", &proto);
	__CATCH_CHAR_ERROR__
	
		LOGE("%s with server [ game-server-1-1 ]", e);

	__TRY_END__
}

